package com.shop.cloud.mall.admin.controller;

import cn.hutool.json.JSONUtil;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.mall.admin.service.ConfigLogisticsService;
import com.shop.cloud.mall.common.entity.ConfigLogistics;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * @date 2018/11/14
 * <p>
 * 物流
 */
@RestController
@AllArgsConstructor
@RequestMapping("/logistics")
@Tag(description = "logistics", name = "物流")
public class LogisticsController {
	private final ConfigLogisticsService configLogisticsService;

	/**
	 * 实时快递查询
	 * @return
	 */
	@SneakyThrows
	@Operation(summary = "实时快递查询")
	@GetMapping
	public R query(QueryTrackParam queryTrackParam) {
		String param = JSONUtil.toJsonStr(queryTrackParam);
		QueryTrackReq queryTrackReq = new QueryTrackReq();
		ConfigLogistics configLogistics = configLogisticsService.getOne();
		queryTrackReq.setParam(param);
		queryTrackReq.setCustomer(configLogistics.getLogisticsCustomer());
		queryTrackReq.setSign(SignUtils.querySign(param ,configLogistics.getLogisticsKey(),configLogistics.getLogisticsCustomer()));
		IBaseClient baseClient = new QueryTrack();
		return R.ok(baseClient.execute(queryTrackReq));
	}
}
